package mymyoux.graphics.controls
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.containers.Box;
	import mx.managers.PopUpManager;
	
	import mymyoux.graphics.GraphicFactory;
	import flash.utils.setTimeout;
	/**
	 * Create an pourcent loader popup.
	 */
	public class PourcentLoader extends Box
	{
		/**
		 * Conteneur IOC.
		 * Besoin du dossier IOC pour fonctionner.
		 */
		protected var conteneur:GraphicFactory;
		/**
		 * @private
		 */
		protected var _title:String="";
		/**
		 * @private
		 */
		protected var _text:String="";
		/**
		 * @private
		 */
		protected var _percent:Number=0;
		/**
		 * @private
		 */
		static protected var _root:DisplayObject;
		/**
		 * Create an pourcent loader popup.
		 * @param root If you want to give an other root.
		 */
		public function PourcentLoader(root:DisplayObject=null)
		{
			super();
			if(root==null && _root!=null)
			{
				init(_root);
			}else
			{
				init(root);
			}
			
		}
		/**
		 * Root of the swf. Need to be completed.
		 */
		static public function set root(value:DisplayObject):void
		{
			_root=value;	
		}
		static public function get root():DisplayObject
		{
			return _root;
		}
		/**
		 * Call by the constructor.
		 */
		protected function init(root:DisplayObject):void
		{
			PopUpManager.addPopUp(this,root,true);			
			conteneur=new GraphicFactory(this);
			conteneur.loadURL("ioc/percent.xml");
			conteneur.addEventListener(GraphicFactory.DISPLAY_READY,onDisplayed);
		}
		/**
		 * Call by the conteneur.
		 * @param event Event dispatched by the weak container.
		 */
		protected function onDisplayed(event:Event):void
		{
			//forte to refresh();
			title=_title;
			text=_text;
			percent=_percent;
			setTimeout(PopUpManager.centerPopUp,200,this);
			//PopUpManager.centerPopUp(this);
		}
		/**
		 * Title of the window.
		 */
		public function get title():String
		{
			return _title;
		}
		public function set title(value:String):void
		{
			_title=value;
			if(conteneur.hasChild("window"))
			{
				conteneur.getChild("window").title=_title;
			}
		}
		/**
		 * Text on the window.
		 */
		public function get text():String
		{
			return _text;
		}
		public function set text(value:String):void
		{
			_text=value;
			if(conteneur.hasChild("label"))
			{
				conteneur.getChild("label").text=_text;
			}
		}
		/**
		 * Pourcentage.
		 */
		public function get percent():Number
		{
			return _percent;
		}
		public function set percent(value:Number):void
		{
			_percent=value;
			if(conteneur.hasChild("progress"))
			{
				try
				{
					conteneur.getChild("progress").setProgress(value,100);
				}catch(error:Error)
				{
					
				}
				
			}
		}
		/**
		 * Kill the window.
		 */
		public function destroy():void
		{
			PopUpManager.removePopUp(this);
			conteneur.destroy();
		}
		
	}
}